package com.uchiha.nova.core.publisher;

import com.uchiha.nova.core.annotation.ApiLog;
import com.uchiha.nova.core.event.ApiLogEvent;
import com.uchiha.nova.core.model.LogApi;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.NovaConstant;
import com.uchiha.nova.core.tool.util.SpringUtil;
import com.uchiha.nova.core.tool.util.WebUtil;
import com.uchiha.nova.core.utils.LogAbstractUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 周林静
 * @Date 2021-10-25 15:00
 * @Version 1.0
 */
@Slf4j
public class ApiLogPublisher {


    public static void publishEvent(String methodName, String methodClass, ApiLog apiLog, long time, String res) {

        HttpServletRequest request = WebUtil.getRequest();
        LogApi logApi = new LogApi();
        logApi.setType(NovaConstant.LOG_NORMAL_TYPE);
        logApi.setResData(res);
        logApi.setTitle(apiLog.value());
        logApi.setTime(String.valueOf(time));
        logApi.setMethodClass(methodClass);
        logApi.setMethodName(methodName);
        LogAbstractUtil.addRequestInfoToLog(request, logApi);
        Map<String, Object> event = new HashMap<>(16);
        event.put(EventConstant.EVENT_LOG, logApi);
        SpringUtil.publishEvent(new ApiLogEvent(event));
    }

}

